/*
 * *****************************************************************************************
 *
 * 		Copyright (C) 2014-2019 Gowin Semiconductor Technology Co.,Ltd.
 * 		
 * @file			main.c
 * @author		Embedded Development Team
 * @version		V1.0.0
 * @date			2019-10-1 09:00:00
 * @brief			Main function.
 ******************************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "gw1ns4c.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

/* Declaraction --------------------------------------------------------------*/
void UartInit(void);

/* Variable ------------------------------------------------------------------*/
int temp_add=0;

int main()
{   
	unsigned char Temp1[100];
	unsigned char receive1[100];
	uint32_t i;
	
	NVIC_InitTypeDef InitTypeDef_NVIC;

	SystemInit();				//system init
	UartInit();					//UART0 init
	I2C_Init(I2C,400);	//i2c init
	
	UART_SendChar(UART0,0xF5);
	
	for(i=0;i<100;i++)
	{
		Temp1[i] =i;
	}
	
	//enable i2c interrupt
	InitTypeDef_NVIC.NVIC_IRQChannel = I2C_IRQn;
	InitTypeDef_NVIC.NVIC_IRQChannelPreemptionPriority = 1;
	InitTypeDef_NVIC.NVIC_IRQChannelSubPriority = 1;
	InitTypeDef_NVIC.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&InitTypeDef_NVIC);

	I2C_InterruptOpen(I2C);
	
	//i2c send bytes
	I2C_SendBytes(I2C ,0x50,0x00,Temp1, 30);
	
	//i2c receive bytes
	I2C_ReadBytes(I2C,0x50,0x00,receive1,30);
	
	while(1);
}

void UartInit(void)
{
  UART_InitTypeDef UART_InitStruct;
	
  UART_InitStruct.UART_Mode.UARTMode_Tx = ENABLE;
  UART_InitStruct.UART_Mode.UARTMode_Rx = ENABLE;
  UART_InitStruct.UART_Int.UARTInt_Tx = DISABLE;
  UART_InitStruct.UART_Int.UARTInt_Rx = DISABLE;
  UART_InitStruct.UART_Ovr.UARTOvr_Tx = DISABLE;
  UART_InitStruct.UART_Ovr.UARTOvr_Rx = DISABLE;
  UART_InitStruct.UART_Hstm = DISABLE;
  UART_InitStruct.UART_BaudRate = 115200;//Baud Rate
	
  UART_Init(UART0,&UART_InitStruct);
}

//I2C Interrupt handler
//Disable in gw1ns4c_it.c
void I2C_Handler(void)
{
	if(~(I2C->SR &I2C_SR_TIP))//Translate Over
	{
		temp_add ++;
		UART_SendChar(UART0,temp_add);	
	}
	
	I2C->CR = I2C_CMD_IACK;//Clear the Interrupt Flag
}
